<script lang="ts" setup>
import * as echarts from "echarts";
import { nextTick, onMounted, ref, shallowRef, watch } from "vue";
import { selectActiveTotal } from "@/api/system.ts";
import { convertTimeFormat } from "@/util";

const chart = shallowRef<any>(null);
const chartContainer = ref(null);
const dataX = ref<any[]>([]);
const dataY = ref<any[]>([]);
const date = ref<string[]>([
  convertTimeFormat(new Date().setDate(new Date().getDate() - 30)),
  convertTimeFormat(new Date()),
]);
const searchWords = ref({
  type: "1",
  endDate: convertTimeFormat(new Date()),
  startDate: convertTimeFormat(new Date().setDate(new Date().getDate() - 30)),
});

const typeList = [
  { value: "1", label: "天" },
  { value: "2", label: "月" },
  { value: "3", label: "年" },
];

async function getSelectActiveTotal() {
  const { data, success } = await selectActiveTotal({
    ...searchWords.value,
  });
  if (!success) {
    return;
  }
  dataX.value = [];
  dataY.value = [];
  data.map((item: any) => {
    dataX.value.push(item.dateTxt);
    dataY.value.push(item.total);
  });
  initChart();
}

function initChart() {
  chart.value = echarts.init(chartContainer.value, "'macarons'");

  chart.value.setOption({
    xAxis: {
      name: "(日期)",
      type: "category",
      data: dataX.value,
    },
    yAxis: {
      name: "(人)",
      type: "value",
    },
    tooltip: {
      trigger: 'axis'
    },
    series: [
      {
        name: '活跃人数',
        data: dataY.value,
        type: "line",
      },
    ],
  });
}

watch(date, (value) => {
  searchWords.value.startDate = value?.[0] ? `${value?.[0]}` : "";
  searchWords.value.endDate = value?.[1] ? `${value?.[1]}` : "";
});
watch(searchWords.value, async () => {
  await getSelectActiveTotal();
});
onMounted(() => {
  getSelectActiveTotal();
  nextTick(() => {});

  window.addEventListener("resize", () => {
    if (chart.value) {
      chart.value.resize();
    }
  });
});
</script>

<template>
  <div class="content">
    <div class="title">活跃度分析</div>
    <div class="date-picker">
      <el-form :inline="true" :model="searchWords" class="search">
        <el-form-item>
          <el-select
            v-model="searchWords.type"
            placeholder="类型"
            style="width: 70px"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-M-D"
            style="width: 200px"
          />
        </el-form-item>
      </el-form>
    </div>
    <div ref="chartContainer" class="chart-container" />
  </div>
</template>

<style scoped lang="scss">
.content {
  position: relative;
  height: calc(100vh - 100px);
}
.chart-container {
  width: 100%;
  height: 50vh;
}
.title {
  width: 150px;
  position: absolute;
  top: 0px;
  font-size: 18px;
  font-weight: 700;
  left: calc(50% - 75px);
}
.date-picker {
  width: 310px;
  position: absolute;
  z-index: 100;
  top: 0px;
  right: 10px;
}
.el-form-item {
  margin-right: 10px;
}
</style>
